package pers.chao.tool.infrastructure;

import pers.chao.tool.math.RandomUtil;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Description: id生成器
 *
 * @author W.Chao
 * @date 2020/11/8 15:37
 **/
public interface IDGenerator<T> {
	T generator();

	// 以下为接口generator()的实现
	IDGenerator<String> UUID = java.util.UUID.randomUUID()::toString;

	IDGenerator<String> SIMPLE_UUID = () -> UUID.generator().replaceAll("-","");

	IDGenerator<String> RANDOM = RandomUtil::random8Char;

	IDGenerator<String> MD5 = () -> {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(UUID.generator().concat(RandomUtil.random8Char()).getBytes());
			return new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	};
}
